package com.databing.demo.base

import android.os.Bundle
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import com.databing.demo.config.INTENT_TITLE
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable


/**
 * 创建日期：2020/11/2
 * 描述:用来控制ActionBar
 * 作者:支豪亮
 */
open class TitleActivity : AppCompatActivity() {

    protected var mCompositeDisposable: CompositeDisposable? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val title = intent.getStringExtra(INTENT_TITLE)
        val action = supportActionBar
        if (!title.isNullOrEmpty() && action != null) {
            action.title = title
            action.setDisplayHomeAsUpEnabled(true)
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            onBackPressed()
            return true;
        }
        return super.onOptionsItemSelected(item)
    }

    protected open fun addSubscribe(subscription: Disposable?) {
        if (mCompositeDisposable == null) {
            mCompositeDisposable = CompositeDisposable()
        }
        mCompositeDisposable!!.add(subscription!!)
    }


    override fun onDestroy() {
        if (mCompositeDisposable != null) {
            mCompositeDisposable!!.dispose();
        }
        super.onDestroy()
    }
}